package com.zjht.gmcc.bean.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.zjht.gmcc.bean.util.MyConstant;
import com.zjht.gmcc.bean.util.MyFormat;

/**
 *  年份下拉框控件
 */
public class SelectYearTag extends TagSupport {

	private static final long serialVersionUID = -8564101591730457334L;
	private String name;// 控件name属性
	private String id;// 控件ID
	private int selected;// 选中值
	private String onchange;// 事件
	private String firstoption;//首选项
	private String firstvalue;//首选项值
	private boolean disabled;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getOnchange() {
		return onchange;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public int getSelected() {
		return selected;
	}

	public void setSelected(int selected) {
		this.selected = selected;
	}
	

	public String getFirstoption() {
		return firstoption;
	}

	public void setFirstoption(String firstoption) {
		this.firstoption = firstoption;
	}
	

	public String getFirstvalue() {
		return firstvalue;
	}

	public void setFirstvalue(String firstvalue) {
		this.firstvalue = firstvalue;
	}
	

	public boolean isDisabled() {
		return disabled;
	}

	public void setDisabled(boolean disabled) {
		this.disabled = disabled;
	}

	@SuppressWarnings("static-access")
	public int doStartTag() throws JspException {

		String name = this.name;
		String id = this.id;
		int selected = this.selected;
		String onchange = this.onchange;
		String firstoption = this.firstoption;
		String firstvalue = this.firstvalue;
		boolean disabled=this.disabled;

		if (name == null || name.length() == 0)
			name = "year";
		if (id == null || id.length() == 0)
			id = name;
		if(firstoption == null || firstoption.length() ==0)
			firstoption = "请选择";
		if(MyFormat.isStrNull(firstvalue)){
			firstvalue=MyConstant.SELECT_FIRST_VALUE;
		}
		StringBuilder html = new StringBuilder();
		html.append("<select name=\"");
		html.append(name);
		html.append("\" ");
		html.append("id=\"");
		html.append(id);
		html.append("\" ");
		if (onchange != null && onchange.length() > 0) {
			html.append("onchange=\"");
			html.append(onchange);
			html.append("\" ");
		}
		if(disabled){
			html.append("disabled");
		}
		html.append(">");
		int thisyear = MyFormat.getYear();//今年
		int lastyear = MyFormat.getYear() - 1;//去年
		int yearbeforelast = lastyear - 1;//前年
		html.append("<option value=\"");
		html.append(firstvalue);
		html.append("\" ");
		html.append(">");
		html.append(firstoption);
		
		html.append("<option value=\"");
		html.append(yearbeforelast);
		html.append("\" ");
		if (yearbeforelast == selected)
			html.append("selected");
		html.append(">");
		html.append(yearbeforelast + "年");
		html.append("<option value=\"");
		html.append(lastyear);
		html.append("\" ");
		if (lastyear == selected)
			html.append("selected");
		html.append(">");
		html.append(lastyear + "年");
		html.append("</option>");
		html.append("<option value=\"");
		html.append(thisyear);
		html.append("\" ");
		if (thisyear == selected)
			html.append("selected");
		html.append(">");
		html.append(thisyear + "年");
		html.append("</option>");
		html.append("</select>");
		try {
			pageContext.getOut().print(html.toString());
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
		html = null;
		return this.SKIP_BODY;
	}
}
